﻿using System.Diagnostics;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Input;

namespace ibba.src.sl.componentes.Codes
{
    /// <summary>
    /// Class to attach mouse click events to UIElements
    /// </summary>
    public class MouseClickManager
    {
        #region Events

        private event MouseButtonEventHandler _click;
        private event MouseButtonEventHandler _doubleClick;

        public event MouseButtonEventHandler Click
        {
            add { _click += value; }
            remove { _click -= value; }
        }

        public event MouseButtonEventHandler DoubleClick
        {
            add { _doubleClick += value; }
            remove { _doubleClick -= value; }
        }

        #endregion


        #region Properties

        private bool Clicked { get; set; }
        public int DoubleClickTimeout { get; set; }

        #endregion


        #region Constructor

        public MouseClickManager(int doubleClickTimeout)
        {
            this.Clicked = false;
            this.DoubleClickTimeout = doubleClickTimeout;
        }

        #endregion


        /// <summary>
        /// Handles the click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        public void HandleClick(object sender, MouseButtonEventArgs e)
        {

            lock (this)
            {
                if (this.Clicked)
                {
                    this.Clicked = false;
                    OnDoubleClick(sender, e);
                }
                else
                {
                    this.Clicked = true;
                    ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
                    Thread thread = new Thread(threadStart);
                    thread.Start(e);
                }
            }

        }

        /// <summary>
        /// Resets the thread.
        /// </summary>
        /// <param name="state">The state.</param>
        private void ResetThread(object state)
        {
            Thread.Sleep(this.DoubleClickTimeout);

            lock (this)
            {
                if (this.Clicked)
                {
                    this.Clicked = false;
                    OnClick(this, (MouseButtonEventArgs)state);
                }
            }

        }


        /// <summary>
        /// Called when [click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnClick(object sender, MouseButtonEventArgs e)
        {
            if (_click != null)
            {
                Debug.Assert(sender is Control);
                (sender as Control).Dispatcher.BeginInvoke(_click, sender, e);
            }

        }

        /// <summary>
        /// Called when [double click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (_doubleClick != null)            
                _doubleClick(sender, e);            

        }


    }


    



}
